home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk2 / undel1 / undelete.jan88 < prev    next >
Text File  |  1995-03-18  |  7KB  |  283 lines

  1. 28-Jan-88 17:07:54-GMT,00007648;000000000000
  2. Date: 28 Jan 88 17:07:54 GMT
  3. Subject: undelete.c  (part 1 of 1)
  4. From: ain@j.cc.purdue.edu (Patrick White)
  5.  
  6. Program Name:    undelete
  7. Submitted By:    bsu-cs!jdh@ee.ecn.purdue.edu (John Hiday)
  8. Author:        James Cooper Jr.
  9.         2104B Rogers Dr.
  10.         Fayetteville, NC 28303
  11. Summary:    undeletes files.
  12. Poster Boy:  Pat White  (ain@j.cc.purdue.edu)
  13. Untested.
  14.  
  15. NOTES:
  16.    There is no binary for this program.
  17.    I have posted the binary version of a *different* undelete program
  18. to comp.binaries.amiga.  These versions are *not* the same.
  19.    This was submitted by someone other that the author -- I did not check
  20. with the author (ie. that he exists).
  21.  
  22. -- Pat White   (co-moderator comp.sources/binaries.amiga)
  23. UUCP: j.cc.purdue.edu!ain  BITNET: PATWHITE@PURCCVM   PHONE: (317) 743-8421
  24. U.S.  Mail:  320 Brown St. apt. 406,    West Lafayette, IN 47906
  25.  
  26. ========================================
  27.  
  28. This Undelete is pretty old, but it does still work.  I got it from the
  29. Amiga directory of a Spring or Fall '86 DECUS SIG tape.  I am not the
  30. author.
  31.  
  32. I have successfully compiled it using Manx 3.4a with +L.  I did have
  33. to comment out the #include "devices/extio.h" line because I don't
  34. have any such file.  Evidently whatever is in it got covered someplace
  35. else because it compiles just fine without it.
  36.  
  37. #    This is a shell archive.
  38. #    Remove everything above and including the cut line.
  39. #    Then run the rest of the file through sh.
  40. #----cut here-----cut here-----cut here-----cut here----#
  41. #!/bin/sh
  42. # shar:    Shell Archiver
  43. #    Run the following text with /bin/sh to create:
  44. #    ud1.c
  45. # This archive created: Thu Jan 28 11:58:28 1988
  46. # By:    Patrick White (PUCC Land, USA)
  47. echo shar: extracting ud1.c '(5504 characters)'
  48. cat << \SHAR_EOF > ud1.c
  49. /* Undelete.c V1.0: A program to recover deleted files.        
  50.  
  51. This program will scan a disk for the given filename, and copy the file
  52. to another disk if found.  It is NOT case sensitive, and will find all
  53. copies of the file on the disk with the same 
  54. name.
  55.  
  56. When the file is copied, the number of the first block of the file is
  57. appended to the name to keep multiple copies from overwriting each other.
  58.  
  59. author: James Cooper Jr.
  60.     2104B Rogers Dr.
  61.     Fayetteville, NC 28303
  62.  
  63. With thanks to:
  64.     Tom Wilcox
  65.     3047 Cameron Way
  66.     Santa Clara, CA 95051
  67.  
  68. for his fixdisk.c program.  This program is given to the public domain, but
  69. please keep the credits intact. */
  70.  
  71. #include "exec/types.h"
  72. #include "exec/nodes.h"
  73. #include "exec/lists.h"
  74. #include "exec/memory.h"
  75. #include "exec/interrupts.h"
  76. #include "exec/ports.h"
  77. #include "exec/libraries.h"
  78. #include "exec/io.h"
  79. #include "exec/tasks.h"
  80. #include "exec/execbase.h"
  81. #include "exec/devices.h"
  82. #include "ctype.h"
  83. #include "devices/trackdisk.h"
  84. #include "stdio.h"
  85.  
  86. #define TD_READ CMD_READ
  87. #define BLOCKSIZE TD_SECTOR
  88.  
  89. struct MsgPort *diskport;
  90. struct IOExtTD *diskreq;
  91.  
  92. #define NAMELENGTH 408
  93. #define FILENAME   409
  94. #define TypeDATA     8
  95.  
  96. struct DataBlock
  97.    {
  98.     LONG  type, key, seqnum, size, next, checksum;
  99.     UBYTE data[BLOCKSIZE-24];
  100.    } DataBlock, *diskdata;
  101.  
  102. FILE *fopen();
  103.  
  104. int blocks = 0;
  105.  
  106. extern struct MsgPort *CreatePort();
  107. extern struct IORequest *CreateExtIO();
  108.  
  109. ULONG diskChangeCount;
  110.  
  111. /*
  112. #include "devices/extio.h"    I have no such file and Manx compiles
  113.                 just fine without it - John Hiday
  114. */
  115.  
  116. char name[80];
  117.  
  118. long ReadBlock (Block, Kind)
  119.    LONG Block;
  120.    char *Kind;
  121. {
  122.    diskreq->iotd_Req.io_Length = BLOCKSIZE;
  123.    diskreq->iotd_Req.io_Data = (APTR) diskdata;
  124.           
  125.      /* show where to put the data when read */
  126.    diskreq->iotd_Req.io_Command = ETD_READ;
  127.                /* check that disk not changed before reading */
  128.    diskreq->iotd_Count = diskChangeCount;
  129.    diskreq->iotd_Req.io_Offset = BLOCKSIZE*Block;
  130.    DoIO(diskreq);
  131.  
  132.    if (diskreq->iotd_Req.io_Error != 0)
  133.       { 
  134.      printf("*** Can't read %s from block %ld; error %ld\n",
  135.         Kind,Block,diskreq->iotd_Req.io_Error); 
  136.      return (FALSE);
  137.       }
  138.    return (TRUE);
  139. }
  140.  
  141. MotorOn()
  142. {
  143.     /* TURN ON DISK MOTOR ... old motor state is returned in io_Actual */
  144.     diskreq->iotd_Req.io_Length = 1;  /* 1 => motor is to be turned on */
  145.     diskreq->iotd_Req.io_Command = TD_MOTOR;   /* operate on the motor */
  146.     DoIO(diskreq);
  147. }
  148.  
  149. MotorOff()
  150. {
  151.     diskreq->iotd_Req.io_Length = 0
  152. ; /* 0 => motor is to be turned off */
  153.     diskreq->iotd_Req.io_Command = TD_MOTOR;   /* operate on the motor */
  154.     DoIO(diskreq);
  155. }
  156.  
  157. CopyFile(FirstBlock)
  158.    LONG FirstBlock;
  159. {
  160.    FILE *file;
  161.    LONG block = FirstBlock;
  162.  
  163.    printf ("\n\n");
  164.  
  165.    sprintf (name, "DF1:%s.%ld", &DataBlock.data [FILENAME], block);
  166.    printf ("Writing file %s\n", name);
  167.  
  168.    if ((file = fopen (name, "w")) == NULL)
  169.    {
  170.        printf ("Cannot open %s\n\n", name);
  171.        return (0);
  172.    }
  173.  
  174.    for (block = FirstBlock; block != 0; block = DataBlock.next)
  175.    { 
  176.      if (!ReadBlock (block, "file data")) break;     
  177.  
  178.      if (fwrite (DataBlock.data, DataBlock.size, 1, file) != 1)
  179.         printf ("*** Can't write data block %ld from disk block %ld\n",
  180.         DataBlock.seqnum, block);
  181.      
  182.      blocks = blocks-1;
  183.    }
  184.  
  185.    if (block==0) { printf ("File %s is complete\n\n", name); }
  186.    else printf ("File %s has been truncated\n\n", name);
  187.  
  188.    fclose (file);
  189. }
  190.  
  191. StrComp(str1, str2)
  192. char *str1, *str2;
  193. {
  194.    int index = 0;
  195.  
  196.    while (1)
  197.    {
  198.       if (str1[index] == 0 && str2[index] == 0) return(0);
  199.       if (toupper(str1[index]) != toupper(str2[index])) return(-1);
  200.       index++;
  201.    }
  202. }
  203.  
  204. main(argc, argv)
  205. int argc;
  206. char **argv;   
  207. {
  208.    LONG block;
  209.    char *lookedfor;
  210.  
  211.    if (argc == 1)
  212.    {
  213.       printf("Usage: Undelete <filename>\n\n");
  214.       exit(0);
  215.    }
  216.  
  217.    lookedfor = argv[1];
  218.  
  219.    diskdata = &DataBlock;       /* point to first location in disk buffer */
  220.    if ((diskport = CreatePort(0,0)) == 0) exit(100);    /* error */
  221.  
  222.    /* make an io request block for communicating with the disk */
  223.    diskreq = (struct IOExtTD *)CreateExtIO(diskport,sizeof(struct IOExtTD));
  224.  
  225.    if(diskreq == 0) { DeletePort(diskport); exit(200); }
  226.  
  227.    /* open the device for access, unit 0 is builtin drive */
  228.    if (0 != OpenDevice(TD_NAME,0,diskreq,0)) exit (150);
  229.  
  230.    printf ("Put disk with deleted file in INTERNAL drive.\n");
  231.    printf ("(Cancel the requester if the disk is unreadable.)\n");
  232.    printf ("Put empty disk in EXTERNAL drive.\n");
  233.    printf ("Press RETURN to begin.\n")
  234. ;
  235.  
  236.    getchar();
  237.  
  238.    /* now get the disk change value */
  239.    diskreq->iotd_Req.io_Command = TD_CHANGENUM;
  240.    DoIO(diskreq);
  241.    diskChangeCount = diskreq->iotd_Req.io_Actual;
  242.  
  243.    printf ("Scanning disk for %s...\n", lookedfor);
  244.  
  245.    for (block = 0; block < 1760; block++)
  246.    {
  247.       if (ReadBlock (block, "information") && DataBlock.type == TypeDATA)
  248.       {
  249.          blocks += 1;
  250.       
  251.    if (DataBlock.seqnum == 1)
  252.      {
  253.         if (ReadBlock(DataBlock.key, "file header"))
  254.         {
  255.            DataBlock.data [FILENAME + DataBlock.data [NAMELENGTH]] = 0;
  256.            sprintf (name, "%s", &DataBlock.data [FILENAME]);
  257.         }
  258.         else
  259.            name[1] = 0;
  260.  
  261.         printf ("Name = %s%cK\r", name, 0x9b);
  262.         if (StrComp(name, lookedfor) == 0) CopyFile (block);
  263.      }
  264.       }
  265.    }
  266.  
  267.    printf ("\n\nAll disk blocks have been scanned.\n");
  268.    MotorOff();
  269.    CloseDevice(diskreq);
  270.    DeleteExtIO(diskreq, sizeof(struct IOExtTD));
  271.    DeletePort(diskport);
  272.    exit(0);
  273. }
  274.  
  275. SHAR_EOF
  276. if test 5504 -ne "`wc -c ud1.c`"
  277. then
  278. echo shar: error transmitting ud1.c '(should have been 5504 characters)'
  279. fi
  280. #    End of shell archive
  281. exit 0
  282.  
  283.